using System;
using System.Collections.Generic;
using System.Text;

namespace RapidHDL
{
    class Encoder : TruthTableComponent 
    {

        public NodeVector InputNodes;
        public NodeVector OutputNodes;


        public Encoder(Component poParentComponent, string psName, int piInputWidth, int piOutputWidth) 
            : base (poParentComponent,psName)
        {            
            InputNodes = this.CreateNodeVector("In", piInputWidth, NodeFlowType.Sink);
            //InputNodes.ForcedNodeStructure = NodeStructure.Input;
			OutputNodes = this.CreateNodeVector("Out",piOutputWidth,NodeFlowType.Source);
            OutputNodes.ForcedNodeStructure = NetStructure.Output;

            this.GetTruthTable("Encoder", piInputWidth, piOutputWidth);

		}

		protected override void GenerateTruthTable()
		{
            int iInputWidth = InputNodes.Nodes.Count;
            int iOutputWidth = OutputNodes.Nodes.Count;

			string sInput;
            string sOutput;

            for (int iIn=0; iIn < iInputWidth; iIn++)
            {
                sOutput = Conversion.IntToBinaryString(iIn,iOutputWidth);
                sInput = new string('0', iInputWidth - 1);
                sInput = sInput.Insert(iIn,"1");
                sInput = Conversion.ReverseString(sInput);
                this.TruthTable.AddTableEntry(sInput, sOutput);
            }
            this.TruthTable.AddTableEntry("default",Conversion.IntToBinaryString(0, iOutputWidth));
		}

		public override bool TransformStructureToVerilog()
		{
			WriteVerilogLookupTable();
			return true;
		}

    }
}
